package com.liwang.lanouhn.nvshengo.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.liwang.lanouhn.nvshengo.Info.DressLiveInfo;
import com.liwang.lanouhn.nvshengo.R;
import com.liwang.lanouhn.nvshengo.adapter.DressLiveAdapter;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 2016/10/10.
 */
public class DressLiveFragment extends Fragment {
    private RecyclerView Recyclerview;
    private DressLiveInfo dressliveinfo;
    private DressLiveAdapter liveAdapter;
    String url ="https://a4.plu.cn/api/ustream/home?version=3.6.4&device=4";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dresslive, null);
        initView(view);
        getDataFromWeb();
        return view;
    }

    private void initView(View view) {
        Recyclerview = (RecyclerView) view.findViewById(R.id.Recyclerview);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(getActivity(),2);
        Recyclerview.setLayoutManager(gridLayoutManager);


    }
    private void getDataFromWeb() {
       new Thread(new Runnable() {
           @Override
           public void run() {
               OkHttpClient client =new OkHttpClient();
               Request request =new Request.Builder().url(url).build();
               Call call =client.newCall(request);
               call.enqueue(new Callback() {
                   @Override
                   public void onFailure(Call call, IOException e) {

                   }

                   @Override
                   public void onResponse(Call call, Response response) throws IOException {
                       String result =response.body().string();
                       Gson gson =new Gson();
                       dressliveinfo=gson.fromJson(result,DressLiveInfo.class);
                       if (dressliveinfo.getData().getStreams().getItems().size()>0){
                           handler.sendEmptyMessage(1);
                       }else {
                           handler.sendEmptyMessage(0);
                       }
                   }
               });
           }
       }).start();

    }
  private  Handler handler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    Toast.makeText(getContext(), "网络请求失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    liveAdapter=new DressLiveAdapter(getActivity(),dressliveinfo.getData().getStreams().getItems());
                    Recyclerview.setAdapter(liveAdapter);
                    break;
            }
        }
    };

}
